//
//  UserInfo.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/9/20.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class UserInfo: NSObject {
    var token:String = ""
    var userID:String = ""
    var phoneNum:String = ""
    var password:String = ""
    var userName:String = ""
    var avatar:String = ""
    var gender:String = ""
    var birthday:String = ""
    var signature:String = ""
    
    init(token:String,userID:String,phoneNum:String,password:String,userName:String,avatar:String,gender:String,birthday:String,signature:String) {
        self.token = token
        self.userID = userID
        self.phoneNum = phoneNum
        self.password = password
        self.userName = userName
        self.avatar = avatar
        self.gender = gender
        self.birthday = birthday
        self.signature = signature
    }
    
    init(coder aDecoder:NSCoder!){
        self.token=aDecoder.decodeObjectForKey("token") as! String
        self.userID=aDecoder.decodeObjectForKey("userID") as! String
        self.phoneNum=aDecoder.decodeObjectForKey("phoneNum") as! String
        self.password=aDecoder.decodeObjectForKey("password") as! String
        self.userName=aDecoder.decodeObjectForKey("userName") as! String
        self.avatar=aDecoder.decodeObjectForKey("avatar") as! String
        self.gender=aDecoder.decodeObjectForKey("gender") as! String
        self.birthday=aDecoder.decodeObjectForKey("birthday") as! String
        self.signature=aDecoder.decodeObjectForKey("signature") as! String
    }
    
    func encodeWithCoder(aCoder:NSCoder!){
        aCoder.encodeObject(token,forKey:"token")
        aCoder.encodeObject(userID,forKey:"userID")
        aCoder.encodeObject(phoneNum,forKey:"phoneNum")
        aCoder.encodeObject(password,forKey:"password")
        aCoder.encodeObject(userName,forKey:"userName")
        aCoder.encodeObject(avatar,forKey:"avatar")
        aCoder.encodeObject(gender,forKey:"gender")
        aCoder.encodeObject(birthday,forKey:"birthday")
        aCoder.encodeObject(signature,forKey:"signature")
    }
}

struct OperateUserDefaults {
    //存储用户信息
    static func saveUserDefault(userInfo:AnyObject){
        let keyedArchiver = NSKeyedArchiver.archivedDataWithRootObject(userInfo)
        NSUserDefaults.standardUserDefaults().setObject(keyedArchiver, forKey: USERINFO)
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: ISLOGIN)
    }
    
    //删除用户信息
    static func deleteUserDefault(){
        NSUserDefaults.standardUserDefaults().removeObjectForKey(USERINFO)
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: ISLOGIN)
    }
    
    //取出用户信息
    static func takeUserInfo()->UserInfo{
        let userDefaults = NSUserDefaults.standardUserDefaults().objectForKey(USERINFO) as! NSData
        let user = NSKeyedUnarchiver.unarchiveObjectWithData(userDefaults) as! UserInfo
        return user
    }
}








